﻿using System;
using System.Globalization;
using System.Web.UI;
using Pantry.Business;
using Pantry.Commons;
using Pantry.Entity;

namespace Pantry.Web.Users
{
    public partial class DistrictDetail : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            var strTrustRegionId = Request.QueryString["TrustRegionId"];
            if (!Validates.IsNaturalNumber(strTrustRegionId))
            {
                Response.Redirect(UrlBackHistory());
                return;
            }

            var strTrustDistrictId = Request.QueryString["TrustDistrictId"];
            var trustDistrictId = Convert.ToInt32(strTrustDistrictId, CultureInfo.CurrentCulture);
            var trustDistrictBus = new TrustDistrictBus();
            var trustDistrict = (TrustDistrict)trustDistrictBus.RetrieveEntity(trustDistrictId);
            if (trustDistrict != null)
            {
                BindDistrictToWeb(trustDistrict);
                LabelTitle.Text = "Update District";
                ButtonSave.Text = "Save";
                if (!trustDistrict.IsActive)
                {
                    trustDistrictBus.ChangeIsActive("TrustDistrict","IsActive","True","TrustDistrictId",trustDistrict.TrustDistrictId);
                }
            }
            else
            {
                InitDataToWeb(strTrustRegionId);
                HiddenFieldTrustRegionId.Value = strTrustRegionId;
                LabelTitle.Text = "Create District";
                ButtonSave.Text = "Insert";
                ButtonInactive.Visible = false;
            }
        }

        public void ButtonBackClicked(object sender,EventArgs eventArgs)
        {
            Response.Redirect(UrlBackHistory());
        }
        
        public void ButtonSaveOnClicked(Object sender, EventArgs eventArgs)
        {
            var trustDistrict = CreateTrustDistrict();
            if (trustDistrict == null)
            {
                return;
            }

            if (trustDistrict.TrustDistrictId != -1)
            {
                UpdateTrustDistrict(trustDistrict);
            }
            else
            {
                InsertTrustDistrict(trustDistrict);
            }
        }

        public void ButtonInactiveClicked(Object sender, EventArgs eventArgs)
        {
            var trustDistrict = new TrustDistrict
                                    {
                                        TrustDistrictId = Convert.ToInt32(HiddenFieldTrustDistrictId.Value,CultureInfo.CurrentCulture),
                                        IsActive = true
                                    };
            if (!trustDistrict.IsActive) return;
            var trustDistrictBus = new TrustDistrictBus();
            trustDistrictBus.ChangeIsActive("TrustDistrict", "IsActive", "False", "TrustDistrictId", trustDistrict.TrustDistrictId);
            Response.Redirect(UrlBackHistory());
        }

        private TrustDistrict CreateTrustDistrict()
        {
            if (String.IsNullOrEmpty(TextBoxDistrictName.Text))
            {
                TextBoxDistrictName.Focus();
                // Notify name's empty
                const string script = @"showmessage('Name of District is empty !','ERROR','','false');";
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "resetDefaultFired", script, true);
                return null;
            }
            //var teamBus = new TeamBus();

            var trustDistrictId = -1;
            if (!String.IsNullOrEmpty(HiddenFieldTrustDistrictId.Value))
            {
                trustDistrictId = Convert.ToInt32(HiddenFieldTrustDistrictId.Value, CultureInfo.InvariantCulture);
            }

            var trustDistrict = new TrustDistrict
            {
                Description = TextBoxDescription.Text,
                TrustDistrictName = TextBoxDistrictName.Text,
                TrustRegionId = Convert.ToInt32(HiddenFieldTrustRegionId.Value,CultureInfo.CurrentCulture),
                TrustDistrictId = trustDistrictId
            };

            return trustDistrict;
        }

        private void UpdateTrustDistrict(object trustDistrict)
        {
            var trustDistrictBus = new TrustDistrictBus();
            string script;
            var updateTrustDistrict = trustDistrictBus.Update(trustDistrict);
            if (updateTrustDistrict == 1)
            {
                // Notify update success
                script = @"showmessage('Update is success !','SUCCESS','" + UrlBackHistory() + "','true');";
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "resetDefaultFired", script, true);
            }

            if (updateTrustDistrict == -1)
            {
                // Notify connection error
                script = @"showmessage('Connection occur a error!','ERROR','','false');";
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "resetDefaultFired", script, true);
            }

            if (updateTrustDistrict == -2)
            {
                TextBoxDistrictName.Focus();
                // Notify exits Name
                script = @"showmessage('Name of District is exits !','ERROR','','false');";
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "resetDefaultFired", script, true);
            }
        }

        private void InsertTrustDistrict(object trustDistrict)
        {
            var trustDistrictBus = new TrustDistrictBus();
            string script;
            var insertTrustDistrict = trustDistrictBus.Insert(trustDistrict);
            if (insertTrustDistrict == 1)
            {
                // Notify update success
                script = @"showmessage('Insert is success !','SUCCESS','" + UrlBackHistory() + "','true');";
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "resetDefaultFired", script, true);
            }

            if (insertTrustDistrict == -1)
            {
                // Notify connection error
                script = @"showmessage('Connection occur a error!','ERROR','','false');";
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "resetDefaultFired", script, true);
            }

            if (insertTrustDistrict != -2) return;
            TextBoxDistrictName.Focus();
            // Notify exits Name
            script = @"showmessage('Name of District is exits !','ERROR','','false');";
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "resetDefaultFired", script, true);
        }

        private void BindDistrictToWeb(TrustDistrict trustDistrict)
        {
            HiddenFieldTrustDistrictId.Value = trustDistrict.TrustDistrictId.ToString(CultureInfo.CurrentCulture);
            HiddenFieldTrustRegionId.Value = trustDistrict.TrustRegionId.ToString(CultureInfo.InvariantCulture);
            TextBoxRegionName.Text = trustDistrict.TrustRegionName;
            TextBoxDistrictName.Text = trustDistrict.TrustDistrictName;
            TextBoxDescription.Text = trustDistrict.Description;
        }

        private void InitDataToWeb(string strTrustRegionId)
        {
            var trustRegionBus = new TrustRegionBus();
            var trustRegionId = Convert.ToInt32(strTrustRegionId, CultureInfo.CurrentCulture);
            var trustRegion = trustRegionBus.RetrieveTrustRegion(trustRegionId);
            if (trustRegion==null)
            {
                Response.Redirect(UrlBackHistory());
                return;
            }

            HiddenFieldTrustRegionId.Value = strTrustRegionId;
            HiddenFieldIsActive.Value = @"True";
            TextBoxRegionName.Text = trustRegion.TrustRegionName;
        }

        private string UrlBackHistory()
        {
            return @"RegionDetail.aspx?TrustRegionId=" + Request.QueryString["TrustRegionId"] + "#table_wTabs-2";
        }
    }
}
